<?php
class shop_productControl extends skymvc{
	public $userid;
	public $skins="shop/index/";
	public $shopdata;
	public $shopid=0;
	public $oc_where;
	public function __construct(){
		parent::__construct();
	}
	
	public function onInit(){
		$this->userid=M("login")->userid;
		$this->smarty->assign("nav","shop_product");
		$this->shopid=$shopid=get_post("shopid","i");
		$this->shopdata=M("shop")->selectRow(array("where"=>" status<10 AND shopid=".$shopid));
		if(empty($this->shopdata)){
			$this->goall("店铺不存在或者被禁止了",1,0,"/index.php");
		}
		$this->skins="shop/".$this->shopdata['tpl'];
		$this->oc_where=M("login")->userid?" (userid=".M("login")->userid." or oc_ssid='".OC_SSID."')  ":"oc_ssid='".OC_SSID."' ";
		 
		$this->oc_where.=" AND siteid=".SITEID;
		$this->smarty->assign(
				array(
				"shopskins"=>"shop/".$this->shopdata['tpl'],
				"shop"=>$this->shopdata
				)
		);
	}
	
	function onDefault(){
		$rscount=true;
		$where=" status<4 AND shopid=".$this->shopid;
		$url="/index.php?m=shop_product";
		$shop_cid=get_post('shop_cid','i');
		if($shop_cid){
			$where.=" AND shop_cid=".$shop_cid;
			$url.="&shop_cid=".$shop_cid;
		}
		$keyword=get('keyword','h');
		if($keyword){
			$where.=" AND title like '%".$keyword."%' ";
			$url.="&keyword=".urlencode($keyword);
		}
		$start=get_post('per_page','i');
		$limit=20;
		$option=array(
			"where"=>$where,
			"start"=>$start,
			"limit"=>$limit,
			"order"=>"last_time DESC"
		);
		$data=M("shop_product")->select($option,$rscount);
		if($data){
			$t_ids=array();
			foreach($data as $k=>$v){
				$t_ids[]=$v['catid'];
			}
			if($t_ids){
				$t_c=M("category")->cat_list(" catid in("._implode($t_ids).")");
			}
			foreach($data as $k=>$v){
				$v['cname']=$t_c[$v['catid']];
				$data[$k]=$v;
			}
		}
		$pagelist=$this->pagelist($rscount,$limit,$url);
		$sdata=array(
			"list"=>$data,
			"rscount"=>$rscount,
			"pagelist"=>$pagelist
		);
		if(get('ajax')){
			$this->goAll("success",0,$sdata);
		}
		$this->smarty->assign($sdata);
		$this->smarty->display($this->skins."shop_product/index.html");
	}
	
	
	function onList(){
		$rscount=true;
		$where=" status<4 AND shopid=".$this->shopid;
		$url="/index.php?m=shop_product";
		$shop_cid=get_post('shop_cid','i');
		if($shop_cid){
			$where.=" AND shop_cid=".$shop_cid;
			$url.="&shop_cid=".$shop_cid;
		}
		$start=get_post('per_page','i');
		$limit=20;
		$option=array(
			"where"=>$where,
			"start"=>$start,
			"limit"=>$limit,
			"order"=>"last_time DESC"
		);
		$data=M("shop_product")->select($option,$rscount);
		if($data){
			$t_ids=array();
			foreach($data as $k=>$v){
				$t_ids[]=$v['catid'];
			}
			if($t_ids){
				$t_c=M("category")->cat_list(" catid in("._implode($t_ids).")");
			}
			foreach($data as $k=>$v){
				$v['cname']=$t_c[$v['catid']];
				$data[$k]=$v;
			}
		}
		$pagelist=$this->pagelist($rscount,$limit,$url);
		$this->smarty->assign(array(
			"list"=>$data,
			"rscount"=>$rscount,
			"pagelist"=>$pagelist
		));
		$this->smarty->display($this->skins."shop_product/list.html");
	}
	
	public function onShow(){
		$id=get('id','i');
		$data=M('shop_product')->selectRow("id=".$id);
		if(empty($data)){
			$this->goALl("参数出错",1);
		}else{
			$product_data=M('shop_product_data')->selectRow("id=".$id);
		}
		$cart=M("shop_order_cart")->selectRow("shopid=".$this->shopid." AND object_id=".$id." AND ".$this->oc_where);
		if($cart){
			$data['incart']=1;
			$data['cart_num']=$cart['amount'];
		}else{
			$data['incart']=0;
			
		}
		/*******获取产品属性*****************/
 
			$data_attr=$attr=array();
			$r=M("shop_product_attr")->selectRow(array("where"=>" id=$id "));
			if($r['attr_cat_id']){
				$attr=M("attribute")->getAttr($r['attr_cat_id']); 
				$data_attr=json_decode(base64_decode($r['attr_content']),true);
			}
			$data['attr']=$attr;
		 
		/*******获取产品属性结束****************************/
		$sdata=array(
			"product"=>$data,
			"data_attr"=>$data_attr,
			"product_data"=>$product_data,
			"shop"=>$this->shopdata,
			"imgsdata"=>M("imgs")->get("shop_product",$data['id'])
		);
		 
		$this->smarty->goassign($sdata);
		 
		$this->smarty->display($this->skins."shop_product/show.html");
	}
	
	
	 
	
	public function onMyLove(){
		
		$userid=M("login")->userid;
		$limit=20;
		$start=get_post("per_page","i");
		$option=array(
			"limit"=>$limit,
			"start"=>$start,
			"order"=>" id DESC",
			"where"=>" userid=".$userid." AND tablename='shop_product' ",
		);
		$rscount=true;
		$data=M("love")->select($option,$rscount);
		 
		if($data){
			foreach($data as $k=>$v){
				$a=M("shop_product")->selectRow(" id=".$v['object_id']."");
				if(empty($a) or $a['status']>98 ){
					M("love")->delete("id=".$v['id']);
				}else{
					$sdata[]=$a;
				}
			}
		}
		$url="/index.php?m=shop_product&a=mylove";
		$pagelist=$this->pagelist($rscount,$limit,$url); 
		$this->smarty->assign(array(
			"list"=>$sdata,
			"rscount"=>$rscount,
			"pagelist"=>$pagelist
		));
		$this->smarty->display("shop_product/mylove.html");
	}
	
	 
	public function onGetId(){
		if(!M("login")->userid){
			exit(json_encode(array("error"=>1,"nologin"=>1)));
		}else{
		$id=M("model_index")->insert(array("tablename"=>"shop_product"));
		M("shop_product")->insert(array("id"=>$id,"dateline"=>time(),"is_temp"=>1,"status"=>99,"userid"=>M("login")->userid));
		M("shop_product_data")->insert(array("id"=>$id,"dateline"=>time()));
		M("shop_product_attr")->insert(array("attr_content"=>" ","dateline"=>time(),"id"=>$id));
		}
		exit(json_encode(array("error"=>0,"id"=>$id)));
	}
	
	 
	public function onattrByCat(){
		 $id=get('id','i');
		 $cat=M("category")->selectRow(array("where"=>" catid=$id "));
		 if($cat['attr_cat_id']){
			
		 	$cat_id=$cat['attr_cat_id'];
			$data=M("attribute")->getAttr($cat_id);
			$this->smarty->assign(array(
				"data"=>$data,
				"attr"=>$attr,
				"attr_cat_id"=>$cat_id
			));
			$this->smarty->display("attr/attr.html"); 
		 }
	 }
	 
	 
	 public function onAttr(){
		
		 $cat_id=get('cat_id','i');
		 $id=get('id','i');
		 $attr=array();
		 if($id){
			$row=M("shop_product_attr")->selectRow(array("where"=>" id=$id AND attr_cat_id=".$cat_id." "));
			
			if($row){ 
				$attr=json_decode(base64_decode($row['attr_content']),true);
				 
			}
			 
		 }
		$data=M("attribute")->getAttr($cat_id);
		
		$this->smarty->assign(array(
			"data"=>$data,
			"attr"=>$attr,
			"attr_cat_id"=>$cat_id
		));
		$this->smarty->display("attr/attr.html");
		
	 }
	 
	 public function onAddClick(){
		$id=get_post('id','i');
		$row=M("shop_product")->selectRow("id=".$id);
		if($row){
			M("shop_product")->update(array("view_num"=>$row['view_num']+1),"id=".$id);
		}
		 
	 }
	
}
?>